import socket
import time
from datetime import datetime, timedelta
from math import ceil

import threading
import asyncio


s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

s.bind(('0.0.0.0',124))

async def run(msg,address):
    from_timestamp = 0
    for x in msg[40:44]:
        from_timestamp = 256 * from_timestamp + x

    delta_timestamp = ceil(time.time()) - (from_timestamp -2208988800)
    l = [0 for i in range(48)]
    to_timestamp = ceil(time.time()) + 2208988800 + delta_timestamp

    for x in range(4):
        l[43-x] = to_timestamp % 256
        to_timestamp //= 256 

    s.sendto(bytes(bytearray(l)), address)
    
l = []

async def main():
    while True:
        msg, address = s.recvfrom(48)
        if msg[0] == -29+256:
            await run(msg,address)

asyncio.run(main())